package com.blb.zy_community.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.zy_community.entity.SysLogininfor;
import com.blb.zy_community.entity.SysUser;
import com.blb.zy_community.service.ISysLogininforService;
import com.blb.zy_community.service.ISysUserService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 日志输出通知类(登录日志)
 */
@Aspect
@Component
public class LogAdvise {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysLogininforService sysLogininforService;

    @Pointcut("execution(* com.blb.zy_community.service.impl.UserDetailsServiceImpl.*(..))")
    public void pointcut() {
    }



//    @AfterThrowing(pointcut = "pointcut()",throwing = "throwable")
//    public void afterThrowing(JoinPoint joinPoint,Exception throwable) {
//        save(throwable);
//        System.out.println("方法异常时");
//    }

    @After("pointcut()")
    public void afterLog(JoinPoint joinPoint) {
        //获取登录用户名
        SysLogininfor sysLogininfor = new SysLogininfor();
        Object[] args = joinPoint.getArgs();
        String username= (String) args[0];
        sysLogininfor.setUserName(username);
        // 利用RequestContextHolder获取HttpServletRequest对象
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
        //IP地址
        String remoteAddr = httpServletRequest.getRemoteAddr();
        //登录地点
        sysLogininfor.setLoginLocation("内网ip");
        //获取浏览器类型
        String browser = UserAgentUtil.getBrowser(httpServletRequest.getHeader("User-Agent"));
        sysLogininfor.setBrowser(browser);
        // 获取操作系统信息
        String os = UserAgentUtil.getOs(httpServletRequest.getHeader("User-Agent"));
        sysLogininfor.setOs(os);
        //登录状态
        SysUser user = userService.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUserName, username));
        if (user == null) {
            sysLogininfor.setStatus("1");
        } else {
            sysLogininfor.setStatus("0");
        }
        //提示消息
        sysLogininfor.setMsg("登陆成功");
        //登录时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        sysLogininfor.setLoginTime(LocalDateTime.parse(formattedDateTime));
        sysLogininforService.save(sysLogininfor);

    }

//    public void save(Exception e){
//        // 记录登录日志
//        SysLogininfor sysLogininfor = new SysLogininfor();
//        // 利用RequestContextHolder获取HttpServletRequest对象
//        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
//        //获取登录的用户名
//
//        String authorization = httpServletRequest.getHeader("Authorization");
//        String username = JwtUtils.getUsernameFromToken(authorization, RsaUtils.publicKey);
//        //用户名
//        sysLogininfor.setUserName(username);
//        // 获取当前请求对象
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        //ip地址
//        String ip = request.getRemoteAddr();
//        sysLogininfor.setIpaddr(ip);
//        //登录地点
//        sysLogininfor.setLoginLocation("内网ip");
//        //获取浏览器类型
//        String browser = UserAgentUtil.getBrowser(request.getHeader("User-Agent"));
//        sysLogininfor.setBrowser(browser);
//        // 获取操作系统信息
//        String os = UserAgentUtil.getOs(request.getHeader("User-Agent"));
//        sysLogininfor.setOs(os);
//
//        SysUser user = userService.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUserName, username));
//        //登录状态
//        if (user == null) {
//            sysLogininfor.setStatus("1");
//        } else {
//            sysLogininfor.setStatus("0");
//        }
//        //提示消息
//        sysLogininfor.setMsg(e.getMessage());
//        //登录时间
//        LocalDateTime now = LocalDateTime.now();
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-d'T'HH:mm:ss");
//        String formattedDateTime = now.format(formatter);
//        sysLogininfor.setLoginTime(LocalDateTime.parse(formattedDateTime));
//        System.out.println("aaa");
//        System.out.println(sysLogininfor);
//        sysLogininforService.save(sysLogininfor);
//    }



}
